﻿/// <reference Path="jquery-1.3.2-vsdoc.js" />
$(function() {
    var co = $("#divContainer"), c = $("#divClassify"), p = $("#divProperty"), pv = $("#divPropertyVal");
    var resize = function() {
        var w = (co.width() - c.width() - 3 * (2 * (5 + 1))) / 2, h = c.height();
        p.width(w); pv.width(w); p.height(h); pv.height(h);
    };
    $(window).resize(resize); resize();

    c.find("select").change(function() {
        var sel = $(this); p.empty();
        $.getJSON(property.copyUrl.replace(/-classifyId-/ig, sel.val()), null, function(o) {
            if (o.length) {
                $(o).each(function(index, object) {
                    var pid = object.PropertyId, inputid = "p" + pid,
                    span = $('<span></span>').attr("class", "pSpan").attr("id", "pSpan" + pid),
                    label = $('<label></label>').attr("for", inputid),
                    input = $('<input type="checkbox" />').attr("value", pid).attr("id", inputid);
                    p.append(span.append(label.append(input).append(object.Name)));
                });
            } else { p.html("未发现记录!"); }

            $("span.pSpan").find("label").click(function() {
                var s = $(this), cb = s.find("input"), div = $("#" + sel.val());
                var pSpan = div.find("#" + s.parent().attr("id"));
                if (cb.attr("checked")) {
                    var url = property.copyValUrl.replace(/-propertyId-/ig, cb.val());
                    $.getJSON(url, null, function(o) {
                        var pvSpan = $('<span class="pvSpan"></span>');
                        if (o.length) {
                            $(o).each(function(index, object) {
                                var pvid = object.PropertyValId, pvinputid = "pv" + pvid,
                                span = $('<span></span>').attr("id", "pvSpan" + pvid),
                                label = $('<label></label>').attr("for", pvinputid).click(function() {
                                    var target = div.find("#" + s.parent().attr("id")).find("span.pvSpan");
                                    if ($(this).find("input").attr("checked")) {
                                        div.show();
                                        if (target[0] == undefined) {
                                            s.parent().clone().appendTo(div);
                                            target = div.find("#" + s.parent().attr("id")).find("span.pvSpan").empty();
                                        }
                                        if (div.find("#pvSpan" + pvid)[0] == undefined) {
                                            target.append($(this).parent().clone()); cb.attr("checked", true);
                                        }
                                    } else {
                                        div.find("#pvSpan" + pvid).remove();
                                        if (target.find("span").length == 0) {
                                            target.parent().remove(); cb.attr("checked", false);
                                        }
                                        if (div.find("span.pSpan").length == 0) { div.hide(); }
                                    }
                                    pv.find(":input").attr("checked", true).attr("disabled", true);
                                }),
                                input = $('<input type="checkbox" />').attr("value", pvid).attr("id", pvinputid).attr("name", "cbKey");
                                pvSpan.append(span.append(label.append(input).append(object.Name)));
                            });
                        } else { pvSpan.append("未发现记录!"); }
                        s.parent().find("span.pvSpan").remove(); $(pvSpan).insertAfter(s);
                        s.parent().find("span.pvSpan").find("input").attr("checked", true);
                        // 添加到复制列表项
                        if (div[0] == undefined) {
                            var cText = unescape(escape(sel.find("option:selected").text()).replace(/%A0/g, "")).replace(/[│├└]+/ig, "");
                            div = $("<div></div>").attr("id", sel.val()).html("<b>" + cText + "</b>"); $(div).appendTo(pv);
                        } else { div.show(); }
                        if (pSpan[0] == undefined) { s.parent().clone().appendTo(div); }
                    });
                } else {
                    s.parent().find("span:pvSpan").remove(); pSpan.remove();
                    if (div.find("span.pSpan").length == 0) { div.hide(); }
                }
            });
            // 加载已加入复制列表的项
            $("#" + sel.val()).find("span.pSpan").each(function() {
                p.find("#" + $(this).attr("id")).find(":input").click();
            });
        });
    });
    // 设置了全局的 AJAX 操作效果，对于 Disabled 必须重新设置
    $(document).ajaxComplete(function() { pv.find(":input").attr("checked", true).attr("disabled", true); });
    $("form").bind("submit", function() {
        var pvids = pv.find(":input");
        if (pvids.length == 0) { alert("未选择要复制的特性值!"); return false; }
        p.find(":input").attr("checked", false); pvids.attr("disabled", false);
    });
});